import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import { ParentMenuRouterWidget } from "@/components/layout"

interface IconItem {
  icon?: string
  children?: RouteRecordItem[]
}

export type RouteRecordItem = IconItem & RouteRecordRaw

export const routes: Array<RouteRecordItem> = [
  {
    path: '/',
    name: 'home',
    icon: "home.svg",
    redirect: '/control/train-control'
  },
  {
    path: '/control',
    name: '系统控制',
    component: ParentMenuRouterWidget,
    icon: "joystick.svg",
    // component: {render: (e) => e("router-view")},
    children: [
      {
        path: "/control/train-control1",
        name: '道岔控制',
        icon: "turnout.svg",
        component: () => import("@/views/ControlView.vue"),
      },
      {
        path: "/control/train-control",
        name: '列车控制',
        icon: "train.svg",
        component: () => import("@/components/pages/train_control/TrainControlPanel.vue"),
      },
    ]
  },
  {
    path: '/configs',
    name: '系统配置',
    component: ParentMenuRouterWidget,
    icon: "configs.svg",
    // component: {render: (e) => e("router-view")},
    children: [
      {
        path: "/configs/controller-config",
        name: '控制器配置',
        icon: "wireless_controller.svg",
        component: () => import("@/components/pages/controller_config/ControllerConfigPanel.vue"),
      },
    ]
  },
  {
    path: '/track',
    name: '轨道图编辑',
    icon: "railroad_map.svg",
    component: () => import("@/components/track_map/TrackMap.vue")
  },
  {
    path: '/program_management',
    name: '程序管理',
    icon: "railroad_map.svg",
    component: () => import("@/components/pages/program_management/ProgramManagement.vue")
  },
  {
    path: '/about',
    name: 'about',
    icon: "info.svg",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/components/HelloWorld.vue')
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  // if (to.matched.some(res => res.meta.requireAuth)) { // 验证是否需要登陆
  //   var id=window.sessionStorage.getItem('userId');
  //   console.log(id)
  //   if (id) { // 查询本地存储信息是否已经登陆
  //     next();
  //   } else {
  //     next({
  //       path: '/login', // 未登录则跳转至login页面
  //       redirect:  '/main2' // 登陆成功后回到当前页面，这里传值给login页面，to.fullPath为当前点击的页面
  //       });
  //   }
  // } else {
  console.log(to, from, next)
  next();

})

export default router
